package com.example.administrator.x3picturetransliteration.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间戳转换
 * 2018/11/5.
 */

public class TimeStampUtil {

    /*
     * 将时间转换为时间戳
     */
    public String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public String dateToMonth(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);

        return stampToDateWithoutHour(date.getTime());
    }

    /*
     * 将时间戳转换为时间 带小时
     */
    public String stampToDate(long lt) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss");
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时间 不带小时
     */
    public String stampToDateWithoutHour(long lt) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时间 不带小时 点
     */
    public String stampToDateWithoutHourPoint(long lt) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 秒转化成分钟 取整
     *
     * @param second
     * @return
     */
    public String secondToMinute(int second) {
        if (second < 60) {
            return "0分钟";
        }
        return second / 60 + "分钟";
    }

    /**
     * 秒转化成天 取整到分钟
     *
     * @param second
     * @return
     */
    public String secondToDay(int second) {
        if (second < 60) {
            return "0天0时0分";
        }
        if (second < 3600) {
            return "0天0时" + second / 60 + "分";
        }
        if (second < 86400) {
            int h = second / 3600;
            int m = (second % 3600) / 60;
            return "0天" + h + "时" + m + "分";
        }
        int d = second / 86400;
        int h = (second % 86400) / 3600;
        int m = ((second % 86400) % 3600) / 60;
        return d + "天" + h + "时" + m + "分";
    }

    public String caonima(int second) {
        if (second < 60) {
            return "时0分" + second + "秒";
        }
        if (second < 3600) {
            return "0时" + second / 60 + "分" + second%60 + "秒";
        }

        int h = second / 3600;
        int m = (second % 3600) / 60;
        return h + "时" + m + "分" + second%60 + "秒";
    }

}
